﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GoodTrader.GTDownload
{
    public class CpSvr8111SDownloader : CpDownLoader
    {
        private double[] arrVal = new double[GTGlobal.MaxDownloadCount];

        // 실시간 프로그램 거래량 수집 객체
        private DSCBO1Lib.CpSvr8111SClass m_CpSvr8111S = new DSCBO1Lib.CpSvr8111SClass();
        public CpSvr8111SDownloader(CpDownLoadManager aDownManager)
            : base(aDownManager)
        {
            m_CpSvr8111S.Received += new DSCBO1Lib._IDibEvents_ReceivedEventHandler(OnReceived);
            m_CpSvr8111S.Subscribe();
        }

        public CpSvr8111SDownloader(CpDownLoadManager aDownManager, CpSvr8111SData aData)
            : base(aDownManager)
        {
            mData = aData;
            m_CpSvr8111S.Received += new DSCBO1Lib._IDibEvents_ReceivedEventHandler(OnReceived);
            m_CpSvr8111S.Subscribe();
        }

        private CpSvr8111SData mData = null;
        public CpSvr8111SData Data
        {
            get { return mData; }
            set { mData = value; }
        }

        private double mEndTime = 0.0;
        public double EndTime
        {
            get { return mEndTime; }
            set { mEndTime = value; }
        }

        private double mToday = 0.0;

        

        private double mOldTime = 0.0;
        
        public override void OnReceived()
        {
            if (mData == null)
                return;

            mToday = Convert.ToDouble(m_CpSvr8111S.GetHeaderValue(0)); // 날짜
            mEndTime = Convert.ToDouble(m_CpSvr8111S.GetHeaderValue(1)); // 시간
            if (GTGlobal.mEndDate == 0.0 || mEndTime == 0.0 || mEndTime > GTGlobal.EndTime)
                return;
            double curTime = Convert.ToDouble(m_CpSvr8111S.GetHeaderValue(1)); // 시간
            double curProfitBuy = Convert.ToDouble(m_CpSvr8111S.GetHeaderValue(13)); // 차익매수
            double curProfitSell = Convert.ToDouble(m_CpSvr8111S.GetHeaderValue(7)); // 차익매도
            double curNonArbitrageBuy = Convert.ToDouble(m_CpSvr8111S.GetHeaderValue(31)); // 비차익매수
            double curNonArbitrageSell = Convert.ToDouble(m_CpSvr8111S.GetHeaderValue(25)); // 비차익매도
            double curProfitPureBuy = curProfitBuy - curProfitSell;
            double curNonArbitragePureBuy = curNonArbitrageBuy - curNonArbitrageSell;
            double curTotalPureBuy = curProfitPureBuy + curNonArbitragePureBuy;

            //Console.WriteLine("8111" + mOldTime.ToString() + "::" + mEndTime.ToString());
            if (mEndTime == mOldTime)
            {
                int endIndex = mData.Time.Length - 1;

                mData.Time[endIndex] = curTime;
                mData.ProfitBuy[endIndex] = curProfitBuy;
                mData.ProfitSell[endIndex] = curProfitSell;
                mData.NonArbitrageBuy[endIndex] = curNonArbitrageBuy;
                mData.NonArbitrageSell[endIndex] = curNonArbitrageSell;
                mData.ProfitPureBuy[endIndex] = curProfitPureBuy;
                mData.NonArbitragePureBuy[endIndex] = curNonArbitragePureBuy;
                mData.TotalPureBuy[endIndex] = curTotalPureBuy;
            }
            else
            {
                int cYear = 0, cMonth = 0, cDay = 0;
                GTGlobal.StockDateToSysDate(mToday, ref cYear, ref cMonth, ref cDay);
                int cHour = 0, cMin = 0, cSec = 0;
                GTGlobal.StockTimeToSysTime(mEndTime, ref cHour, ref cMin, ref cSec);
                DateTime curDate = new DateTime(cYear, cMonth, cDay, cHour, cMin, cSec);
                // 최종적으로 받은 시간을 확인해 1분씩 작은 값들을 모두 할당해 준다.
                for (int i = GTGlobal.MaxDownloadCount - 1, j = 0; i > -1; i--, j++)
                {
                    mData.SysTime[i] = curDate.AddMinutes(-j);
                }

                // 원래의 값들을 하나씩 밀어 준다.
                GTGlobal.ShiftArrayVal(curTime, mData.Time, arrVal);
                GTGlobal.ShiftArrayVal(curProfitBuy, mData.ProfitBuy, arrVal);
                GTGlobal.ShiftArrayVal(curProfitSell, mData.ProfitSell, arrVal);
                GTGlobal.ShiftArrayVal(curNonArbitrageBuy, mData.NonArbitrageBuy, arrVal);
                GTGlobal.ShiftArrayVal(curNonArbitrageSell, mData.NonArbitrageSell, arrVal);
                GTGlobal.ShiftArrayVal(curProfitPureBuy, mData.ProfitPureBuy, arrVal);
                GTGlobal.ShiftArrayVal(curNonArbitragePureBuy, mData.NonArbitragePureBuy, arrVal);
                GTGlobal.ShiftArrayVal(curTotalPureBuy, mData.TotalPureBuy, arrVal);
            }
            mOldTime = mEndTime;
        }
    }
}
